from peewee import (
    AutoField,
    IntegerField,
    CharField,
    BooleanField,
    TextField,
    ForeignKeyField,
    DateTimeField,
    DoubleField,
)
from datetime import datetime
from mxupy import EntityX
import bigOAINet as bigo
import OBEvolutionAI as obe


# 学校
class School(EntityX):
    schoolId = AutoField()
    name = CharField(null=False)

    class Meta:
        database = bigo.db
        name = "学校"


# 学院
class College(EntityX):
    collegeId = AutoField()
    name = CharField(null=False)

    school = ForeignKeyField(
        School, backref="colleges", column_name="schoolId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "学院"


# 系
class Dept(EntityX):
    deptId = AutoField()
    name = CharField(null=False)

    college = ForeignKeyField(
        College, backref="depts", column_name="collegeId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "系"


# 专业表
class Major(EntityX):
    majorId = AutoField()  
    name = CharField(null=False)
    dept = ForeignKeyField(
        Dept, backref="majors", column_name="deptId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "专业"


# 用户表
class Student(EntityX):
    studentId = AutoField()

    user = ForeignKeyField(bigo.User, backref='students', column_name='userId', on_delete='CASCADE')

    stuNum = CharField()

    school = ForeignKeyField(
        School,
        backref="students",
        column_name="schoolId",
        on_delete="CASCADE",
        null=True,
    )
    college = ForeignKeyField(
        College,
        backref="students",
        column_name="collegeId",
        on_delete="CASCADE",
        null=True,
    )
    dept = ForeignKeyField(
        Dept, backref="students", column_name="deptId", on_delete="CASCADE", null=True
    )

    major = ForeignKeyField(
        Major, backref="students", column_name="majorId", on_delete="CASCADE", null=True
    )

    class Meta:
        database = bigo.db
        name = "学生"


# 专业老师表，多对多，一个专业多个老师，一个老师多个专业
class MajorTeacher(EntityX):
    majorTeacherId = AutoField()

    user = ForeignKeyField(
        bigo.User, backref="majorTeachers", column_name="userId", on_delete="CASCADE"
    )
    major = ForeignKeyField(
        Major, backref="majorTeachers", column_name="majorId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "专业老师"


# 课程表
class Course(EntityX):

    courseId = AutoField()
    # 下面多出一个课程老师表，指定课程与老师多对多关联。此字段取消。teacherId=IntegerField()# 老师ID，对应user表
    name = CharField(null=False)
    desc = CharField(null=False)
    logo = CharField(null=False)

    isOpen = IntegerField(default=0)  # 是否公开，0-非公开，1-公开 默认0
    startTime = DateTimeField(null=True)  # 课程开始时间
    endTime = DateTimeField(null=True)  # 课程结束时间
    createTime = DateTimeField(default=datetime.now)

    creater = ForeignKeyField(
        bigo.User, backref="courses_created", column_name="createrId", on_delete="SET NULL", null=True
    )  # 添加创建者字段

    robotUser = ForeignKeyField(
        bigo.User, backref="courses", column_name="robotUserId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "课程"


# 课程老师表
class CourseTeacher(EntityX):
    courseTeacherId = AutoField()

    user = ForeignKeyField(
        bigo.User, backref="courseTeachers", column_name="userId", on_delete="CASCADE"
    )
    course = ForeignKeyField(
        Course, backref="courseTeachers", column_name="courseId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "课程老师"


# 学生选修课程表
class CourseElective(EntityX):
    courseElectiveId = AutoField()

    student = ForeignKeyField(
        Student,
        backref="courseElectives",
        column_name="userId",
        on_delete="CASCADE",
    )
    course = ForeignKeyField(
        Course, backref="courseElectives", column_name="courseId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "选课"


# 课堂表
class Schedule(EntityX):
    scheduleId = AutoField()

    name = CharField(null=False)
    invitationCode = CharField(null=False)
    createTime = DateTimeField(default=datetime.now)

    course = ForeignKeyField(
        Course, backref="schedules", column_name="courseId", on_delete="CASCADE"
    )
    robotUser = ForeignKeyField(
        bigo.User, backref="schedules", column_name="robotUserId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "课堂"

# 班级表
class Class(EntityX):
    classId = AutoField()
    schedule = ForeignKeyField(
        Schedule, backref="classes", column_name="scheduleId", on_delete="CASCADE"
    )
    name = CharField(null=False)
    desc = CharField(null=True)
    createTime = DateTimeField(default=datetime.now)

    class Meta:
        database = bigo.db
        name = "班级"

# 课堂用户表
class ScheduleUser(EntityX):
    scheduleUsersId = AutoField()
    # 角色：teacher:老师、assistant:助教、student:学生
    role = CharField()

    user = ForeignKeyField(
        bigo.User, backref="scheduleUsers", column_name="userId", on_delete="CASCADE"
    )
    schedule = ForeignKeyField(
        Schedule, backref="scheduleUsers", column_name="scheduleId", on_delete="CASCADE"
    )
    # class是关键字，所以使用clazz
    clazz = ForeignKeyField(
        Class, backref="scheduleUsers", column_name="classId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "课堂学生"


# 讨论表
class Discuss(EntityX):
    discussId = AutoField()

    title = CharField()  # 标题
    answer = CharField()  # 讨论的标准答案
    criteria = CharField(null=True)  # 讨论的评分标准
    createTime = DateTimeField(default=datetime.now)

    creater = ForeignKeyField(
        bigo.User, backref="discusses", column_name="createrId", on_delete="CASCADE"
    )
    course = ForeignKeyField(
        Course, backref="discusses", column_name="courseId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "讨论"


# 学生答案
class DiscussUser(EntityX):
    discussUserId = AutoField()

    answer = TextField()  # 答案详情
    aiScore = DoubleField()  # AI评分
    aiEvaluation = CharField(null=True)  # 评价
    score = DoubleField()  # 老师评分
    evaluation = CharField(null=True)  # 评价

    createTime = DateTimeField(default=datetime.now)
    fileName = CharField()  # 真文件名
    filePath = CharField()  # uuid文件名

    discuss = ForeignKeyField(
        Discuss,
        backref="discussUsers",
        column_name="discussId",
        on_delete="CASCADE",
    )
    # 在哪个课堂讨论的
    schedule = ForeignKeyField(
        Schedule,
        backref="discussUsers",
        column_name="scheduleId",
        on_delete="CASCADE",
    )
    student = ForeignKeyField(
        Student,
        backref="discussUsers",
        column_name="userId",
        on_delete="CASCADE",
    )

    class Meta:
        database = bigo.db
        name = "用户讨论"


# 作业表
class Assignment(EntityX):
    assignmentId = AutoField()

    name = CharField()
    answer = CharField()  # 作业标准答案
    criteria = CharField(null=True)  # 评分规则
    createTime = DateTimeField(default=datetime.now)

    creater = ForeignKeyField(
        bigo.User, backref="assignments", column_name="createrId", on_delete="CASCADE"
    )
    course = ForeignKeyField(
        Course, backref="assignments", column_name="courseId", on_delete="CASCADE"
    )

    class Meta:
        database = bigo.db
        name = "作业"


# 学生提交课堂作业记录表
class AssignmentUser(EntityX):
    assignmentUserId = AutoField()

    fileName = CharField()  # 真文件名
    filePath = CharField()  # uuid文件名

    answer = TextField()  # 作业答案
    aiScore = DoubleField()  # AI评分
    aiEvaluation = CharField(null=True)  # 评价
    score = DoubleField()  # 老师评分
    evaluation = CharField(null=True)  # 评价

    createTime = DateTimeField(default=datetime.now)

    student = ForeignKeyField(
        Student,
        backref="assignmentUsers",
        column_name="userId",
        on_delete="CASCADE",
    )  # 提交作业者
    schedule = ForeignKeyField(
        Schedule,
        backref="assignmentUsers",
        column_name="scheduleId",
        on_delete="CASCADE",
    )
    assignment = ForeignKeyField(
        Assignment,
        backref="assignmentUsers",
        column_name="assignmentId",
        on_delete="CASCADE",
    )  # 属于哪个作业

    class Meta:
        database = bigo.db
        name = "用户作业"


# 试题表
class TestItem(EntityX):
    testItemId = AutoField()

    title = CharField()  # 添加这一行 - 试题标题
    content = CharField()  # 试题内容，
    answer = CharField()  # 试题标准答案，单选a，多选abc、判断t/f、填空aaa|bbb
    criteria = CharField(null=True)  # 评分规则
    type = (
        CharField()
    )  # 试题类型：单选题choice,多选题mchoice,判断题judge,填空题blank,计算题calc,简答题mblank,论述题/作文题essay
    createTime = DateTimeField(default=datetime.now)

    creater = ForeignKeyField(
        bigo.User, backref="tests", column_name="createrId", on_delete="CASCADE"
    )  # 创建者
    course = ForeignKeyField(
        Course, backref="tests", column_name="courseId", on_delete="CASCADE"
    )  # 课程

    class Meta:
        database = bigo.db
        name = "试题"


# 学生答案
class TestItemUser(EntityX):
    testItemUserId = AutoField()

    answer = CharField()
    aiScore = DoubleField()  # AI评分
    aiEvaluation = CharField(null=True)  # 评价
    score = DoubleField()  # 老师评分
    evaluation = CharField(null=True)  # 评价

    createTime = DateTimeField(default=datetime.now)

    fileName = CharField()  # 真文件名
    filePath = CharField()  # uuid文件名

    student = ForeignKeyField(
        Student, backref="testItemUsers", column_name="userId", on_delete="CASCADE"
    )
    testItem = ForeignKeyField(
        TestItem,
        backref="testItemUsers",
        column_name="testItemId",
        on_delete="CASCADE",
    )
    schedule = ForeignKeyField(
        Schedule,
        backref="testItemUsers",
        column_name="scheduleId",
        on_delete="CASCADE",
    )

    class Meta:
        database = bigo.db
        name = "用户试题"

 #签到表
class CheckIn(EntityX):
    checkInId = AutoField()

    name = CharField()  # 签到名称
    desc = CharField(null=True)  # 签到描述
    latitude = DoubleField()  # 签到地点纬度
    longitude = DoubleField()  # 签到地点经度
    radius = IntegerField()  # 签到范围（米）
    endTime = DateTimeField()  # 签到截止时间
    createTime = DateTimeField(default=datetime.now)

    creater = ForeignKeyField(
        bigo.User, backref="checkIns", column_name="createrId", on_delete="CASCADE"
    )  # 创建者（老师）
    schedule = ForeignKeyField(
        Schedule, backref="checkIns", column_name="scheduleId", on_delete="CASCADE"
    )  # 课堂

    class Meta:
        database = bigo.db
        name = "签到"


# 学生签到记录表
class CheckInUser(EntityX):
    checkInUserId = AutoField()

    latitude = DoubleField(null=True)  # 签到地点纬度
    longitude = DoubleField(null=True)  # 签到地点经度
    distance = IntegerField(null=True)  # 签到时距离签到点的距离（米）
    createTime = DateTimeField(default=datetime.now)

    checkIn = ForeignKeyField(
        CheckIn,
        backref="checkInUsers",
        column_name="checkInId",
        on_delete="CASCADE",
    )  # 关联的签到
    student = ForeignKeyField(
        Student,
        backref="checkInUsers",
        column_name="userId",
        on_delete="CASCADE",
    )  # 签到的学生

    class Meta:
        database = bigo.db
        name = "用户签到"
# # 聊天室表
# class ChatRoom(EntityX):
#     chatRoomId = AutoField()

#     name = CharField(null=False)
#     schedule = ForeignKeyField(Schedule, backref='chatRooms', column_name='scheduleId', on_delete='CASCADE')
# # 聊天用户表
# class ChatRoomUser(EntityX):
#     chatRoomUserId = AutoField()

#     user = ForeignKeyField(User, backref='chatRoomUsers', column_name='userId', on_delete='CASCADE')
#     chatRoom = ForeignKeyField(ChatRoom, backref='chatRoomUsers', column_name='chatRoomId', on_delete='CASCADE')
# # 聊天记录表
# class ChatRecord(EntityX):
#     chatRecordId = AutoField()

#     chatType = CharField()  # 聊天类型 private：个人助理     group: 课堂
#     targetId = IntegerField()
#     createTime = DateTimeField(default=datetime.now)  # 时间

#     user = ForeignKeyField(User, backref='chatRecordes', column_name='userId', on_delete='CASCADE')
# # 群聊聊天记录表
# class ChatRoomMessage(EntityX):
#     chatRoomMessageId = AutoField()

#     message = TextField(null=False)  # 聊天消息
#     type = CharField()  # text image audio video
#     data = TextField()  # 额外信息
#     uuid = CharField()

#     userRole = CharField()  # assistant user
#     createTime = DateTimeField(default=datetime.now)  # 时间

#     chatRoom = ForeignKeyField(ChatRoom, backref='chatRoomMessages', column_name='chatRoomId', on_delete='CASCADE')
#     user = ForeignKeyField(User, backref='chatRoomMessages', column_name='userId', on_delete='CASCADE')
# # 私聊聊天记录表
# class ChatMessage(EntityX):
#     chatMessageId = AutoField()

#     message = TextField(null=False)  # 聊天消息
#     type = CharField()  # normal:普通消息 register_apply:注册申请 invite:邀请
#     data = TextField()  # 额外的消息数据，json字符串
#     uuid = CharField()

#     status = IntegerField(default=0)  # 0:未读 1:已读/已完成/已处理
#     createTime = DateTimeField(default=datetime.now)  # 时间

#     fromUser = ForeignKeyField(User, backref='chatMessages', column_name='fromUserId', on_delete='CASCADE')
#     toUser = ForeignKeyField(User, backref='chatMessages', column_name='toUserId', on_delete='CASCADE')

# class News(EntityX):
#     newsId = AutoField()

#     title = CharField()  # 标题
#     author = CharField()  # 作者
#     content = TextField()  # 正文
#     category = CharField()  # 分类
#     newsTime = DateTimeField(default=datetime.now)  # 时间
# # 新闻附件表
# class NewsAttachment(EntityX):
#     # 名称，全路径，类型，下载次数，newsId，添加时间
#     newsAttachmentId = AutoField()

#     name = CharField()
#     path = CharField()
#     basePath = CharField()
#     type = CharField()

#     downloadNum = IntegerField(default=0)
#     createTime = DateTimeField(default=datetime.now)

#     news = ForeignKeyField(News, backref='newsAttachments', column_name='newsId', on_delete='CASCADE')


# # 知识库表
# class Knowledge(EntityX):
#     knowledgeId = AutoField()

#     nickname = CharField()
#     role = CharField()
#     limit = CharField()
#     updateTime = DateTimeField(default=datetime.now)
#     createTime = DateTimeField(default=datetime.now)

#     course = ForeignKeyField(
#         Course, backref="knowledges", column_name="courseId", on_delete="CASCADE"
#     )


# # 知识库文件分类表
# class KnowledgeCategory(EntityX):
#     knowledgeCategoryId = AutoField()
#     name = CharField()  # 分类名称


# # 知识库文件表
# class KnowledgeFile(EntityX):
#     knowledgeFileId = AutoField()

#     fileType = CharField()  # 知识库文件类型  video image audio txt pdf docx

#     name = CharField()
#     desc = CharField()
#     fileName = CharField()  # 知识库文件名
#     path = CharField()  # 知识库文件路径
#     basePath = CharField()  # 知识库文件全路径

#     isSync = IntegerField(default=3)  # 是否同步，1未同步，2已同步，3其他资源不需要同步
#     embeddingId = IntegerField(null=True)  # 向量化后的ID
#     createTime = DateTimeField(default=datetime.now)

#     knowledge = ForeignKeyField(
#         Knowledge,
#         backref="knowledgeFiles",
#         column_name="knowledgeId",
#         on_delete="CASCADE",
#     )
#     knowledgeCategory = ForeignKeyField(
#         KnowledgeCategory,
#         backref="knowledgeFiles",
#         column_name="knowledgeCategoryId",
#         on_delete="CASCADE",
#     )


# # 知识点文件表
# class KnowledgePointFile(EntityX):

#     knowledgePointFileId = AutoField()

#     name = CharField()
#     desc = CharField()
#     fileName = CharField()
#     filePath = CharField()
#     fileBasePath = CharField()

#     course = ForeignKeyField(
#         Course,
#         backref="knowledgePointFiles",
#         column_name="courseId",
#         on_delete="CASCADE",
#     )


# # 知识点表
# class KnowledgePoint(EntityX):
#     knowledgePointId = AutoField()

#     title = CharField()
#     level = IntegerField()

#     course = ForeignKeyField(Course, backref="knowledgePoints", on_delete="CASCADE")
#     knowledgePointFile = ForeignKeyField(
#         KnowledgePointFile,
#         backref="knowledgePoints",
#         column_name="knowledgePointFileId",
#         on_delete="CASCADE",
#     )


# # 用户聊天记录收藏表
# class UserFav(EntityX):
#     userFavId = AutoField()

#     uuid = CharField()
#     type = CharField(max_length=255)  # 聊天记录类型  group=群聊 private=私聊
#     createTime = DateTimeField(default=datetime.now)

#     user = ForeignKeyField(
#         User, backref="userFavs", column_name="userId", on_delete="CASCADE"
#     )


# # 群公告表
# class Notice(EntityX):
#     noticeId = AutoField()

#     content = CharField()  # 内容
#     addTime = DateTimeField(default=datetime.now)  # 时间

#     schedule = ForeignKeyField(
#         Schedule, backref="notices", column_name="scheduleId", on_delete="CASCADE"
#     )


# # 毕业指标点
# class graduationTarget(EntityX):
#     graduationTargetId = AutoField()

#     parentId = IntegerField()
#     serialNumber = CharField()

#     college = ForeignKeyField(
#         College,
#         backref="graduationTargets",
#         column_name="collegeId",
#         on_delete="CASCADE",
#     )


# # 课程目标达成度
# class courseTarget(EntityX):
#     courseTargetId = AutoField()

#     desc = CharField()
#     course = ForeignKeyField(
#         Course, backref="courseTargets", column_name="courseId", on_delete="CASCADE"
#     )


# # 投票
# class Vote(EntityX):
#     voteId = AutoField()

#     name = CharField(max_length=255)
#     desc = TextField(null=True)

#     addTime = DateTimeField(default=datetime.now)

#     startTime = DateTimeField(default=datetime.now)
#     endTime = DateTimeField(null=True)
#     isActive = BooleanField(default=True)
#     isSingleChoice = BooleanField(default=True)

#     # vote:投票 raise_hand:举手
#     type = CharField(default="vote")

#     schedule = ForeignKeyField(
#         Schedule, backref="votes", column_name="scheduleId", on_delete="CASCADE"
#     )
#     user = ForeignKeyField(
#         User, backref="votes", column_name="userId", on_delete="CASCADE"
#     )


# # 投票项
# class VoteItem(EntityX):
#     voteItemId = AutoField()

#     name = CharField(max_length=255)
#     desc = TextField(null=True)
#     posts = IntegerField(default=0)

#     vote = ForeignKeyField(
#         Vote, backref="voteItems", column_name="voteId", on_delete="CASCADE"
#     )


# # 用户投票
# class VotePost(EntityX):
#     votePostId = AutoField()

#     ip = CharField(max_length=255)
#     addTime = DateTimeField(default=datetime.now)

#     user = ForeignKeyField(
#         User, backref="votePosts", column_name="userId", on_delete="CASCADE"
#     )
#     vote = ForeignKeyField(
#         Vote, backref="votePosts", column_name="voteId", on_delete="CASCADE"
#     )
#     voteItem = ForeignKeyField(
#         VoteItem, backref="votePosts", column_name="voteItemId", on_delete="CASCADE"
#     )


# 课堂 讨论 关联表   --8.31 删除
# class ScheduleDiscuss(EntityX):
#     scheduleDiscussId = AutoField()
#     discuss = ForeignKeyField(Discuss, backref='scheduleDiscusses', on_delete='CASCADE')
#     schedule = ForeignKeyField(Schedule, backref='scheduleDiscusses', on_delete='CASCADE')  # 讨论属于哪个课堂
#     createTime = DateTimeField(default=datetime.now)

# 课堂 作业 关联表   --8.31 删除
# class ScheduleAssignment(EntityX):
#     scheduleAssignmentId = AutoField()
#     schedule = ForeignKeyField(Schedule, backref='scheduleAssignment', on_delete='CASCADE')  # 属于哪个课堂
#     assignment = ForeignKeyField(Assignment, backref='scheduleAssignment', on_delete='CASCADE')
#     createTime = DateTimeField(default=datetime.now)

# 试题 课堂 关联表   --8.31 删除
# class ScheduleTest(EntityX):
#     scheduleTestId = AutoField()
#     schedule = ForeignKeyField(Schedule, backref='scheduleTests', on_delete='CASCADE')
#     test = ForeignKeyField(Test, backref='scheduleTests', on_delete='CASCADE')
#     createTime = DateTimeField(default=datetime.now)
